package com.sky.module.system.userloginrecord.aspect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.module.system.apistatistics.dao.ApiStatisticsMapper;
import com.sky.module.system.apistatistics.domain.entity.ApiStatisticsEntity;
import com.sky.module.system.user.domain.entity.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Date;


/***
 * @description Api累计调用次数
 *
 * @param null
 * @return
 * @date 2023/3/12 10:12
 *
 */
@Component
@Aspect
public class ApiStatisticsAspect {

    @Autowired
    private ApiStatisticsMapper apiStatisticsMapper;

    @Before("@annotation(org.springframework.web.bind.annotation.PostMapping)")
    public void before(JoinPoint joinPoint) {
        // 如果 是post请求的就加一
//        joinPoint.getSignature().get
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long id = user.getId();
        // 方法名称
        String methodName = joinPoint.getSignature().getName();
        ApiStatisticsEntity apiStatisticsEntity = new ApiStatisticsEntity();
        apiStatisticsEntity.setUserId(id);
        apiStatisticsEntity.setRecordTime(new Date());
        apiStatisticsEntity.setMethod(methodName);
        apiStatisticsMapper.insert(apiStatisticsEntity);

    }
}
